package io.emqx.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.util.StringUtils;
import sun.security.provider.MD5;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MqttSample {
    public static void main(String[] args) {
        String topic = "1_";
        String content = "Hello World";
        int qos = 1;
        String port = "1883";
        String broker = "tcp://localhost:"+port;
//        String broker = "tcp://broker.emqx.io:1883";
        String clientId = MqttClient.generateClientId();
        //  持久化
        MemoryPersistence persistence = new MemoryPersistence();
        // MQTT 连接选项
        MqttConnectOptions connOpts = new MqttConnectOptions();
//        connOpts.setConnectionTimeout(10);
//        connOpts.setAutomaticReconnect(true);
        // 设置认证信息
        connOpts.setUserName("mt5_user");
        connOpts.setPassword(md5("Qwer1234").toCharArray());


        try (MqttClient client = new MqttClient(broker, clientId, persistence)) {
            // 设置回调
            client.setCallback(new SampleCallback());
            client.connect(connOpts);
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            client.publish(topic, message);
            client.disconnect();
            client.close();
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }
    public static String encrypt(String content, String protocol) {
        if (StringUtils.isEmpty(content)) {
            throw new IllegalArgumentException("String to encrypt cannot be null or zero length");
        }
        try {
            MessageDigest md = MessageDigest.getInstance(protocol);
            md.update(content.getBytes());
            return byte2Hex(md.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("NoSuchAlgorithmException", e);
        }
    }
    static String byte2Hex(byte[] bts) {
        if (bts == null || bts.length <= 0) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        for (byte b : bts) {
            result.append(HEX_CHAR[b >> 4 & 0xF]);
            result.append(HEX_CHAR[b & 0xF]);
        }
        return result.toString();
    }
    public static final char[] HEX_CHAR = "0123456789abcdef".toCharArray();
    public static String md5(String content) {
        return encrypt(content, "MD5");
    }
}

